package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.ITravelCommendService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.template.DestinationTemplate;
import cn.wolfcode.luowowo.search.template.StrategyTemplate;
import cn.wolfcode.luowowo.search.template.TravelTemplate;
import cn.wolfcode.luowowo.search.template.UserInfoTemplate;
import cn.wolfcode.luowowo.search.vo.DataVO;
import cn.wolfcode.luowowo.vo.StatisVO;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class IndexController {

    @Reference
    private ITravelCommendService travelCommendService;

    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private ITravelService travelService;

    @Reference
    private IStrategyTemplateService strategyTemplateService;

    @Reference
    private IUserInfoTemplateService userInfoTemplateService;

    @Reference
    private IDestinationTemplateService destinationTemplateService;

    @Reference
    private ITravelTemplateService travelTemplateService;

    @Reference
    private ISearchService searchService;

    /**
     * 首页
     *
     * @param request
     * @return
     */
    @RequestMapping("")
    public String login(@UserParam UserInfo userInfo ,  Model model, HttpServletRequest request) {

        if (userInfo.getPhone() != null) {
            request.getSession().setAttribute("userInfo",userInfo);
        }
        //游记推荐
        model.addAttribute("tcs", travelCommendService.queryCommendTop5());
        //攻略排行的第一个
        model.addAttribute("sc", strategyCommendService.queryCommendTop5().get(0));
        return "index/index";
    }

    /**
     * 首页游记列表
     *
     * @param model
     * @param qo
     * @return
     */
    @RequestMapping("index/travelPage")
    public String travelPage(Model model, @ModelAttribute("qo") TravelQuery qo) {
        model.addAttribute("pageInfo", travelService.queryForList(qo));
        return "index/travelPageTpl";
    }

    @RequestMapping("q")
    public String SearchAll(@ModelAttribute("qo") SearchQueryObject qo, Model model) {
        switch (qo.getType()) {
            case SearchQueryObject.SEARCH_FOR_DESTINATION:
                return searchForDestination(qo, model);
            case SearchQueryObject.SEARCH_FOR_STRATEGY:
                return searchForStrategy(qo, model);
            case SearchQueryObject.SEARCH_FOR_TRAVEL:
                return searchForTravel(qo, model);
            case SearchQueryObject.SEARCH_FOR_USER:
                return searchForUser(qo, model);
            default:
                return searchForAll(qo, model);
        }
    }

    private String searchForUser(SearchQueryObject qo, Model model) {
        Page<UserInfoTemplate> us = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME,
                UserInfoTemplate.class, qo, "nickname", "destName");
        model.addAttribute("page", us);
        return "index/searchUser";
    }

    private String searchForTravel(SearchQueryObject qo, Model model) {
        Page<TravelTemplate> ts = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME,
                TravelTemplate.class, qo, "title", "summary");
        model.addAttribute("page", ts);
        return "index/searchTravel";
    }

    private String searchForStrategy(SearchQueryObject qo, Model model) {
        Page<StrategyTemplate> sts = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME,
                StrategyTemplate.class, qo, "subTitle", "title", "summary");
        model.addAttribute("page", sts);
        return "index/searchStrategy";
    }

    private String searchForAll(SearchQueryObject qo, Model model) {
        Page<UserInfoTemplate> us = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME,
                UserInfoTemplate.class, qo, "nickname", "destName");

        Page<TravelTemplate> ts = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME,
                TravelTemplate.class, qo, "title", "summary");

        Page<StrategyTemplate> sts = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME,
                StrategyTemplate.class, qo, "subTitle", "title", "summary");

        Page<DestinationTemplate> ds = searchService.searchWithHighlight(DestinationTemplate.INDEX_NAME, DestinationTemplate.TYPE_NAME,
                DestinationTemplate.class, qo, "name", "info");

        int total = (int) (us.getTotalElements() + ts.getTotalElements() + sts.getTotalElements() + ds.getTotalElements());
        DataVO data = new DataVO();
        data.setTravels(ts.getContent());
        data.setStrategys(sts.getContent());
        data.setUsers(us.getContent());
        data.setDests(ds.getContent());
        data.setTotal(total);
        model.addAttribute("data", data);
        return "index/searchAll";
    }

    private String searchForDestination(SearchQueryObject qo, Model model) {
        DestinationTemplate dest = destinationTemplateService.findByName(qo.getKeyword());
        if (dest != null) {
            //攻略  destId
            DataVO data = new DataVO();
            int total = 0;
            List<StrategyTemplate> strategys = strategyTemplateService.findByDestId(dest.getId());
            total += strategys.size();
            if (strategys.size() > 5) {
                strategys = strategys.subList(0, 5);
            }
            //游记
            List<TravelTemplate> travels = travelTemplateService.findByDestId(dest.getId());
            total += travels.size();
            if (travels.size() > 5) {
                travels = travels.subList(0, 5);
            }
            //用户
            List<UserInfoTemplate> users = userInfoTemplateService.findByDestName(dest.getName());
            total += users.size();
            if (users.size() > 5) {
                users = users.subList(0, 5);
            }
            data.setStrategys(strategys);
            data.setTravels(travels);
            data.setUsers(users);
            data.setTotal(total);
            model.addAttribute("data", data);
        }
        model.addAttribute("dest", dest);
        return "index/searchDest";
    }


}
